package com.yh.serviceimpl;

import com.yh.dao.PermissionDao;
import com.yh.dao.RoleDao;
import com.yh.dao.RolePermissionDao;
import com.yh.javabean.Permission;
import com.yh.javabean.Role;
import com.yh.javabean.Role_Pid;
import com.yh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: als
 * @Date: 2024/5/27 - 05 - 27 - 16:13
 * @Description: com.yh.serviceimpl
 * @version: 1.0
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    PermissionDao permissionDao;
    @Autowired
    RolePermissionDao rolePermissionDao;

    @Override
    public List<Role> findAll() {
        return roleDao.selectAll();
    }

    @Override
    public void addRole(Role role) {
        roleDao.insertRole(role);
    }

    @Override
    public Role findById(int id) {
        return roleDao.selectById(id);
    }

    @Override
    public void editRole(Role role) {
        roleDao.updateRole(role);
    }

    @Override
    public List<Permission> PermissionAdd() {
        return permissionDao.selectAll();
    }

    @Override
    public List<Permission> permissionMy(int rid) {
        return permissionDao.selectByRid(rid);
    }

    @Override
    @Transactional
    public void editPermission(Role_Pid rp) {
        //删除原本角色的权限
        rolePermissionDao.deleteByRid(rp.getRid());
        //批量添加新的权限
        rolePermissionDao.insertArr(rp.getRid(), rp.getIds());
    }

//    删除角色
    @Override
    @Transactional
    public void deleteRole(int rid) {
        rolePermissionDao.deleteByRid(rid);
        roleDao.deleteById(rid);
    }


    //角色批量删除
    @Override
    public void deleteIds(List<Integer> ids) {
        roleDao.deleteByList(ids);
    }


}
